package com.liu.qqRobot.poke.handler;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.common.utils.HtmlToImageUtils;
import com.liu.common.utils.QQClientUtils;
import com.liu.mybatisPlus.poke.entity.base.Poke;
import com.liu.mybatisPlus.poke.entity.base.PokeGroupUserPoke;
import com.liu.mybatisPlus.poke.service.PokeGroupUserPokeService;
import com.liu.mybatisPlus.poke.service.PokeService;
import com.liu.mybatisPlus.poke.service.PokeUserPokeSkillService;
import com.liu.mybatisPlus.q.service.QGroupUserOperationQuantityService;
import com.liu.qqRobot.event.entity.dto.EventReportingDTO;
import com.liu.qqRobot.poke.entity.templateParam.MyPokeTemplateParam;
import com.liu.qqRobot.q.enums.OperationTypeEnum;
import freemarker.template.TemplateException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Component
public class MyPokeHandler {

    @Resource
    private PokeService pokeService;

    @Resource
    private PokeGroupUserPokeService pokeGroupUserPokeService;

    @Resource
    private QGroupUserOperationQuantityService qGroupUserOperationQuantityService;

    @Resource
    private PokeUserPokeSkillService pokeUserPokeSkillService;

    /**
     * 处理事件
     * @param data 信息内容
     * @param groupId 群号
     * @param userId 发送人QQ号
     */
    public void handler(EventReportingDTO.Message.Data data, Long groupId, Long userId, Boolean isGroup) {
        QueryWrapper<PokeGroupUserPoke> pokeGroupUserPokeQueryWrapper = new QueryWrapper<>();
        pokeGroupUserPokeQueryWrapper.eq("group_id", groupId);
        pokeGroupUserPokeQueryWrapper.eq("user_id", userId);
        pokeGroupUserPokeQueryWrapper.eq("is_release", false);
        List<PokeGroupUserPoke> pokeGroupUserPokeList = pokeGroupUserPokeService.list(pokeGroupUserPokeQueryWrapper);
        if (CollUtil.isEmpty(pokeGroupUserPokeList)) {
            return;
        }
        List<Integer> pokeIdList = pokeGroupUserPokeList.stream().map(PokeGroupUserPoke::getPokeId).collect(Collectors.toList());
        List<Poke> pokeList = pokeService.listByIds(pokeIdList);
//        List<Integer> userPokeIdList = pokeGroupUserPokeList.stream().map(PokeGroupUserPoke::getId).collect(Collectors.toList());
//        List<PokeUserPokeSkillDTO> pokeUserPokeSkillDTOList = pokeUserPokeSkillService.getPokeSkillByUserPokeId(userPokeIdList);
//        Map<Integer, List<PokeUserPokeSkillDTO>> pokeUserPokeSkillDTOListMap = pokeUserPokeSkillDTOList.stream().collect(Collectors.groupingBy(PokeUserPokeSkillDTO::getPokeId));
        List<MyPokeTemplateParam> myPokeTemplateParams = new ArrayList<>();
        pokeList.forEach(poke -> {
            MyPokeTemplateParam myPokeTemplateParam = new MyPokeTemplateParam();
            myPokeTemplateParam.setPokeName(poke.getName());
//            List<PokeUserPokeSkillDTO> pokeUserPokeSkillDTOS = pokeUserPokeSkillDTOListMap.get(poke.getId());
//            if (CollUtil.isNotEmpty(pokeUserPokeSkillDTOS)) {
//                List<String> skillNameList = pokeUserPokeSkillDTOS.stream().map(PokeUserPokeSkillDTO::getSkillName).collect(Collectors.toList());
//                myPokeTemplateParam.setPokeSkillNameList(skillNameList);
//            }
            myPokeTemplateParams.add(myPokeTemplateParam);
        });
        Map<String, Object> templateParam = new HashMap<>();
        templateParam.put("myPokeTemplateParams", myPokeTemplateParams);
        try {
            HtmlToImageUtils.htmlToImage("myPokeTemplate.html", templateParam, "src/main/resources/picture/myPoke.png", 1100);
            File file = new File("src/main/resources/picture/myPoke.png");
            String encode = Base64.encode(file);
            String msg = "[CQ:at,qq=" + userId + "]\n[CQ:image,file=base64://" + encode+ ",type=show,subType=0]";
            QQClientUtils.sendMsg(msg, userId, groupId, isGroup);
            qGroupUserOperationQuantityService.statisticsOperationQuantityByType(groupId, userId, OperationTypeEnum.MY_POKE);
        } catch (IOException | ParserConfigurationException | SAXException | TemplateException e) {
            log.error("获取我的宝可梦展示模板失败:{}", e.getMessage(), e);
//            QQClientUtils.sendGroupMsg("[CQ:at,qq=" + userId + "]\n获取捕捉宝可梦展示模板失败，请联系制作人", userId, groupId, isGroup);
        }
    }
}
